package com.api.framework.websocket;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

@Getter
@Setter
@Slf4j
public class MessageMoel {
    private String to;
    private String from;
    private String id;
    /**
     * 是否发送组
     */
    private Boolean group;
    private JSONObject data;
    /**
     * webSocket 状态
     */
    private String type;
    private String target;
    private String message;

    public MessageMoel(String message) {
        JSONObject json = JSON.parseObject(message);
        this.to = json.containsKey("to") ? json.getString("to") : null;
        this.from = json.containsKey("from") ? json.getString("from") : null;
        this.id = json.containsKey("id") ? json.getString("id") : null;
        this.group = json.containsKey("group") ? json.getBoolean("group") : null;
//        this.data = JSON.parseObject(json.getString("data"));
//        this.type = data.getString("type");
//        this.target = data.getString("target");
//        this.message = data.getString("message");
    }


    /**
     * 返回失败信息
     *
     * @return
     */
    public JSONObject returnError() {
        return mkReturnMess(false);
    }

    /**
     * 返回成功信息
     *
     * @return
     */
    public JSONObject returnSuccess() {
        return mkReturnMess(true);
    }

    /**
     * 创建返回信息
     *
     * @param flag ：true 成功信息，false 失败信息
     * @return
     */
    public JSONObject mkReturnMess(Boolean flag) {
        JSONObject rJo = new JSONObject();
//        JSONObject rJoData = new JSONObject();
        rJo.put("to", this.from);
        rJo.put("from", "mapper/system");
        rJo.put("id", UUID.randomUUID());
        String strType = flag ? "success" : "error";
        rJo.put("type", strType);
        rJo.put("target", this.id);
        String strMess = flag ? "Send Message success." : "Send Message have error. The client cant\'t did found.";
        rJo.put("message", strMess);
//        rJo.put("data",rJoData);
        return rJo;
    }

    public Boolean getGroup() {
        return group;
    }

    public void setGroup(Boolean group) {
        this.group = group;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public JSONObject getData() {
        return data;
    }

    public void setData(JSONObject data) {
        this.data = data;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
